<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addMessage('add')"
            round>
            添加面经
          </el-button>
        </div>
      </template>
      <!--
        el-table 属性
          data: 表格数据, 必须是一个数组
        el-table-column
          width: 宽度, 如果不设置表示自适应
          label: 表头的文字
          prop: 字符串类型, 要在此列渲染的数据属性名
       -->
      <el-table
        :data="list"
        style="width: 100%">
        <el-table-column
          label="标题"
          prop="stem"
          width="400"
        ></el-table-column>
        <el-table-column
          label="作者"
          prop="creator"
          width="140"
        ></el-table-column>
        <el-table-column
          label="点赞"
          prop="likeCount"
          width="140"
        ></el-table-column>
        <el-table-column
          label="浏览数"
          prop="views"
          width="140"
        ></el-table-column>
        <el-table-column
          label="更新时间"
          prop="createdAt"
          width="200"
        ></el-table-column>
         <el-table-column
      fixed="right"
      label="操作"
      width="120">
      <!-- //作用域插槽:获取组件内的数据在父组件中操作 -->
      <template #default={row}>
          <div class="actions">
            <i class="el-icon-view" @click="addMessage('preview',row.id)"></i>
            <i class="el-icon-edit-outline" @click="addMessage('edit',row.id)"></i>
            <i class="el-icon-delete" @click="deleteRow(row.id)"></i>
          </div>
      </template>
    </el-table-column>
      </el-table>
      <!--
        current-page： 当前页码值
        :total：总条数
        layout:页面分页布局
        -->
        <!-- 页码条 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="q.current"
      :page-sizes="[10, 15, 20, 30]"
      :page-size="q.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 抽屉组件
    动态设置title属性 -->
    <el-drawer

  :title="drawerTitle"
  :visible.sync="drawer"
  direction="rtl"
  :before-close="handleClose">
  <div v-if="drawerType === 'preview'" class="article-preview">
  <h5>{{ form.stem }}</h5>
  <div v-html="form.content"></div>
</div>
<el-form v-else :model="form" :rules="rules" ref="form" label-width="80px">
  <el-form-item label="标题" prop="stem">
    <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
  </el-form-item>
  <el-form-item label="内容" prop="content">
    <!-- validateField相当于调用表单校验中部分校验的方法 -->
  <quill-editor v-model="form.content" @blur="$refs.form.validateField('content')"></quill-editor>

  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submit">确认</el-button>
    <el-button>取消</el-button>
  </el-form-item>
</el-form>

</el-drawer>

  </div>
</template>
<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import { createArticle, getArticles, getArticleDetail, removeArticle, changeArticle } from '@/api/article'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      // current: 1,
      // pageSize: 10
      // currentPage4: 7,
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          // 自定义表单校验
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }

        ]
      },
      q: {
        current: 1,
        pageSize: 10
      },
      list: [],
      total: 0,
      drawer: false, // 控制抽屉的显示和隐藏
      drawerType: 'add'// 保存的时候组件操作根据type类型来改变

    }
  },
  created () {
    this.loadArticles()
  },
  computed: {
    // 动态设置抽屉组件标题
    drawerTitle () {
      let title = '添加面经'
      if (this.drawerType === 'add') title = '添加面经'
      if (this.drawerType === 'preview') title = '面经预览'
      if (this.drawerType === 'edit') title = '编辑面经'

      return title
    }
  },
  methods: {
    async submit () {
      // 通过ref 获取form对象实现兜底校验
      try {
        // 兜底校验
        await this.$refs.form.validate()
        // 发送请求
        let res = null
        // 通过判断type类型，判断是增加还是对数据进行编辑
        if (this.drawerType === 'add') {
          const res = await createArticle(this.form)
          console.log(res, 'createArticle')
        } else {
          // 实现编辑功能
          // 将form中的数据进行解构
          const { id, stem, content } = this.form
          res = await changeArticle({ id, stem, content })
          console.log(res, 'changeArticle')
        }

        // 请求成功与失败提醒
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新渲染页面
        // 将页码值改成1
        this.q.current = 1
        this.loadArticles()

        // 提交数据关闭抽屉
        this.handleClose()
      } catch (error) {
        console.log(error)
      }
    },
    handleClose () {
      // 重置表单的校验规则，以及表单数据
      if (this.drawerType !== 'preview') {
        this.$refs.form.resetFields()
      }
      // 手动重置表单数据
      this.form = {
        stem: '',
        content: ''
      }
      this.drawer = false
    },

    async addMessage (type, id) {
    // 打开抽屉组件
      this.drawerType = type

      this.drawer = true
      // console.log(type)
      // console.log(id)
      // 数据回显
      if (type !== 'add') {
        const res = await getArticleDetail(id)
        console.log(res, 'getArticleDetail')
        this.form = res.data
        // const obj = this.list.find(item => item.id === id)
        // console.log(obj)
        // this.form.stem = obj.stem
        // this.form.content = obj.content
      }
    },
    // 获取列表的数据进行渲染
    async  loadArticles () {
      const res = await getArticles(this.q)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 页面条数发生改变时触发
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      // 在触发该事件时，页面条数发生改变，应当让页码数从一开始
      this.q.current = 1
      this.q.pageSize = val
      // 重新发送请求，重新获取数据

      this.loadArticles()
    },
    // 当前的页码值，在切换时 所触发的事件
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      // 改变q对象中的current值
      this.q.current = val
      // 重新发送请求，重新获取数据
      this.loadArticles()
    },
    async deleteRow (id) {
      const res = await removeArticle(id)
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.loadArticles()
    }

  }
}

</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
